package com.kbbywyclient.util.pre;

import android.content.Context;

import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.User;


/**
 * Created by FHW
 * on 2017/3/30
 * Description: Preferences工具类
 */
public class PreUtils {
    /**
     * 是否登录
     *
     * @param context
     * @param loginFlag
     */
    public static void setLogin(Context context, boolean loginFlag) {
        PreferencesUtils.putBoolean(context, PreKey.USER_LOGIN, loginFlag);
    }

    /**
     * 设置是否登录
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        return PreferencesUtils.getBoolean(context, PreKey.USER_LOGIN);
    }

    /**
     * 设置第一次注册，除非用户卸载，否则调用此方法后，进入注册页面都不是第一次注册
     * @param context
     */
    public static void setFirstRegister(Context context){
        PreferencesUtils.putBoolean(context,PreKey.FIRST_REGISTER,true);
    }

    /**
     * 判断是否是第一次注册
     * @param context
     * @return
     */
    public static boolean isFirstRegister(Context context){
        return !PreferencesUtils.getBoolean(context,PreKey.FIRST_REGISTER);
    }

    /**
     * 保存用户头像
     *
     * @param context
     * @param avatar
     */
    public static void setAvatar(Context context, String avatar) {
        PreferencesUtils.putString(context, PreKey.USER_AVATAR, avatar);
    }

    /**
     * 获取用户头像
     *
     * @param context
     * @return
     */
    public static String getAvatar(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_AVATAR);
    }

    /**
     * 保存业务员真实姓名
     *
     * @param context  context
     * @param trueName 业务员真实姓名
     */
    public static void setTrueName(Context context, String trueName) {
        PreferencesUtils.putString(context, PreKey.USER_TRUE_NAME, trueName);
    }

    /**
     * 读取业务员真实姓名
     *
     * @param context context
     */
    public static String getTrueName(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_TRUE_NAME);
    }

    /**
     * 保存业务员认证状态
     *
     * @param context context
     * @param status  status
     */
    public static void setCertificationStatus(Context context, int status) {
        PreferencesUtils.putInt(context, PreKey.USER_CERTIFICATION_STATUS, status);
    }

    /**
     * 业务员是否已验证
     *
     * @param context context
     * @return
     */
    public static boolean isAuthenticated(Context context) {
        return PreferencesUtils.getInt(context, PreKey.USER_CERTIFICATION_STATUS) == 1;
    }

    /**
     * 是否显示引导
     *
     * @param context
     * @return
     */
    public static boolean isGuide(Context context) {
        return PreferencesUtils.getBoolean(context, PreKey.GUIDE_FLAG, true);
    }

    /**
     * 设置是否显示引导
     *
     * @param context
     * @param flag
     */
    public static void setGuide(Context context, boolean flag) {
        PreferencesUtils.putBoolean(context, PreKey.GUIDE_FLAG, flag);
    }


    /**
     * 保存版本号
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        return PreferencesUtils.getInt(context, PreKey.VERSION_CODE);
    }

    /**
     * 设置是否显示引导
     *
     * @param context
     */
    public static void setVersionCode(Context context, int code) {
        PreferencesUtils.putInt(context, PreKey.VERSION_CODE, code);
    }

    /**
     * 设置 记住密码
     *
     * @param context
     * @param remember
     */
    public static void setRemember(Context context, boolean remember) {
        PreferencesUtils.putBoolean(context, PreKey.USER_REMEMBER, remember);
    }

    /**
     * 是否记住密码
     *
     * @param context
     * @return
     */
    public static boolean isRemember(Context context) {
        return PreferencesUtils.getBoolean(context, PreKey.USER_REMEMBER, false);
    }

    public static void setUserAccount(Context context, String account) {
        PreferencesUtils.putString(context, PreKey.USER_ACCOUNT, account);
    }

    public static String getUserAccount(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_ACCOUNT, "");
    }

    public static void setUserPwd(Context context, String pwd) {
        PreferencesUtils.putString(context, PreKey.USER_PWD, pwd);
    }

    public static String getUserPwd(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_PWD, "");
    }

    public static void setBusinessId(Context context, int type) {
        PreferencesUtils.putInt(context, PreKey.BUSINESS_ID, type);
    }

    public static int getBusinessId(Context context) {
        return PreferencesUtils.getInt(context, PreKey.BUSINESS_ID, 0);
    }


    public static void setCompanyID(Context context, String companyID) {
        PreferencesUtils.putString(context, PreKey.COMPANY_ID, companyID);
    }

    public static String getCompanyID(Context context) {
        return PreferencesUtils.getString(context, PreKey.COMPANY_ID, "");
    }

    public static void setCompanyTypeID(Context context, String companyID) {
        PreferencesUtils.putString(context, PreKey.COMPANY_TYPE_ID, companyID);
    }

    public static String getCompanyTypeID(Context context) {
        return PreferencesUtils.getString(context, PreKey.COMPANY_TYPE_ID, "");
    }

    public static void setCityName(Context context, String cityName) {
        PreferencesUtils.putString(context, PreKey.USER_CITY_NAME, cityName);
    }

    public static String getCityName(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_CITY_NAME, "广州市");
    }

    public static void setCity(Context context, int city) {
        PreferencesUtils.putInt(context, PreKey.USER_CITY, city);
    }

    public static int getCity(Context context) {
        return PreferencesUtils.getInt(context, PreKey.USER_CITY, -1);
    }

    public static void setisOpenPacket(Context context, int isOpen) {
        PreferencesUtils.putInt(context, PreKey.USER_IS_OPEN_RPACKET, isOpen);
    }

    public static int getisOpenPacket(Context context) {
        return PreferencesUtils.getInt(context, PreKey.USER_IS_OPEN_RPACKET, -1);
    }

    public static void setDoTask(Context context){
        PreferencesUtils.putBoolean(context,PreKey.CLICK_DO_TASK,true);
    }

    public static boolean hasDoTask(Context context){
        return PreferencesUtils.getBoolean(context,PreKey.CLICK_DO_TASK);
    }

    /**
     * 保存用户信息
     *
     * @param user
     * @param context
     */
    public static void saveUser(Context context, User user) {
        PreferencesUtils.putInt(context, PreKey.USER_SALESID, user.getSalesID());
        if (user.getCity() instanceof Integer) {
            PreferencesUtils.putInt(context, PreKey.USER_CITY, (Integer) user.getCity());
        }
        PreferencesUtils.putString(context, PreKey.USER_CITY_NAME, user.getCityName());
        PreferencesUtils.putString(context, PreKey.USER_GO_TO_TYPE, user.getGoToType());
        PreferencesUtils.putInt(context, PreKey.USER_IS_OPEN_RPACKET, user.getIsOpenRpacket());
        PreferencesUtils.putInt(context,PreKey.USER_DISTRIBUTION_ROLE,user.getRole());
    }

    /**
     * 获取用户信息
     *
     * @param context
     */
    public static User getUser(Context context) {
        User user = new User(PreferencesUtils.getInt(context, PreKey.USER_SALESID),
                PreferencesUtils.getInt(context, PreKey.USER_CITY),
                PreferencesUtils.getString(context, PreKey.USER_CITY_NAME, "广州市"),
                PreferencesUtils.getString(context, PreKey.USER_GO_TO_TYPE),
                PreferencesUtils.getInt(context, PreKey.USER_IS_OPEN_RPACKET, 2),
                PreferencesUtils.getInt(context,PreKey.USER_DISTRIBUTION_ROLE, Constants.LEVEL_NORMAL)
        );

        return user;
    }

    /**
     * 清除用户信息
     *
     * @param context
     */
    public static void clearUser(Context context) {
        PreferencesUtils.putInt(context, PreKey.USER_SALESID, -1);
        PreferencesUtils.putInt(context, PreKey.USER_CITY, -1);
        PreferencesUtils.putString(context, PreKey.USER_CITY_NAME, "");
        PreferencesUtils.putString(context, PreKey.USER_GO_TO_TYPE, "");
        PreferencesUtils.putInt(context, PreKey.USER_IS_OPEN_RPACKET, 2);
        PreferencesUtils.putInt(context,PreKey.USER_DISTRIBUTION_ROLE,Constants.LEVEL_NORMAL);
        PreferencesUtils.putString(context,PreKey.USER_TRUE_NAME,"");
        setLogin(context, false);
    }

    public static void setFirstToPromotion(Context context){
        PreferencesUtils.putBoolean(context,PreKey.USER_FIRST_TO_PROMOTION_PAGE,false);
    }

    public static boolean isFirstToPromotion(Context context){
        return PreferencesUtils.getBoolean(context,PreKey.USER_FIRST_TO_PROMOTION_PAGE,true);
    }
}
